package two.chapter_1.c1_2;

import edu.princeton.cs.algs4.StdOut;

import java.util.Arrays;

public class Ex1_2_9 {


    public static void main(String[] args) {
        Counter counter=new Counter("cmp");
        int a[]={3,6,7,2,3,4,5,4,2,7,9,0};
        Arrays.sort(a);
        int key=3;
        rank(key,0,a.length-1,a,counter);
        StdOut.println(counter);
    }

    private static int rank(int key, int lo, int hi, int[] a,Counter counter) {

        while (lo<=hi){
            int mid=lo+(hi-lo)/2;
            int cmp=key-a[mid];
            counter.increment();
            if (cmp>0){
                lo=mid+1;
            }else if (cmp==0){
                return mid;
            }else {
                hi=mid-1;
            }
        }
        return -1;
    }


}
